#include "OutStreamImpl.h"

OutStreamImpl::OutStreamImpl(void)
{
	m_mid_Write = 0;
	m_mid_Rewind = 0;
}

OutStreamImpl::~OutStreamImpl(void)
{
}

bool OutStreamImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_Write = (env)->GetMethodID(m_cls, "write", "([B)I");
		if(!m_mid_Write)
			break;

		m_mid_Rewind = (env)->GetMethodID(m_cls, "rewind", "()I");
		if(!m_mid_Rewind)
			break;

		result = true;

	} while (false);

	return result;
}

bool OutStreamImpl::Write(const void *buf,int len)
{
	int result = 0;
	JNIEnv* env = 0;
	if(before(&env))
	{
		jbyteArray array = JniHelper::c2j_bytes(env, (unsigned char*)buf, len);
		result = (env)->CallBooleanMethod(m_obj, m_mid_Write, array) != 0;

		after(env);
	}
	return result!=0;
}

int OutStreamImpl::Rewind()
{
	int result = 0;
	JNIEnv* env = 0;
	if(before(&env))
	{
		result = (env)->CallIntMethod(m_obj, m_mid_Rewind);

		after(env);
	}
	return result;
}